// 引入axios 和 qs
import axios from "axios";
import Qs from "qs";
import {
    encrypt,
    decrypt
} from "@/fetch/crypto.js";

// 引入本地存储
import local from "@/utils/local";
let keyy = "lizxlylwkdqzymyzsqblt666";
let IVv = "txwl888txwl20201";
let str = "";

console.log(local.get("Code"), '2222222');

// 环境的切换

axios.defaults.baseURL = 'http://120.77.174.236:8002/';
let baseUrl = "http://120.77.174.236:8002/"

// 请求超时时间
axios.defaults.timeout = 10000;

// post请求头
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';


if (local.get("Code") != null) {
    str = `xstxwl.${local.get("t-k")}.${local.get("Code").Id}.${
    local.get("Code").LoginCode
  }.txwl999`;
	let cc = encrypt(str);
	console.log(cc,'xxxxxxxxxxxxxxxxxxxxxxxxxxx')
    let mtoken = str
    localStorage.setItem('mtoken', mtoken);
    console.log(mtoken, '1221212');
    // 请求拦截器
}


axios.interceptors.request.use((config) => {
	console.log(config,'1212121')
    if (config.method == 'post') {
        config.data = Qs.stringify(config.data)
    }

    let mtoken = encrypt(str, keyy, IVv);
	console.log(mtoken,'加密11111111111111111')
    if (mtoken) {
        config.headers.mtoken = mtoken; //如果要求携带在请求头中
    }

    return config;
});

axios.interceptors.response.use(response => {
    // 定时刷新access-token
    console.log(response,'11111111111111111111111111111111')
    return response
  },
  error => {
    return Promise.reject(error)
  }
)

// export function get(url, params) {
//     return new Promise((resolve, reject) => {
//         axios.get(url, {
//                 params: params
//             })
//             .then(res => {
//                 resolve(res.data);
//             })
//             .catch(err => {
//                 reject(err.data)
//             })
//     }).catch(err => {

//     })
// }


// export function post(url, params) {
//     return new Promise((resolve, reject) => {

//         axios.post(url, params)
//             .then(res => {
//                 resolve(res.data);
//             })
//             .catch(err => {
//                 reject(err.data)
//             })
//     }).catch(err => {

//     })
// }

function request({
    otherUrl,
    url,
    method,
    data,
    params,
    header,
}) {
    return new Promise((resolve, reject) => {
        return axios.request({
            url: (otherUrl || baseUrl) + url,
            data,
            method,
            params,
            header

        }).then(res => {
            // console.log(res, 'rrrrrrrrrrrr');
            //  返回数据及处理
            if (res.data.Code == 200) {

                resolve(res.data)

            } else if (res.data.Code == 403) {
                console.log("权限");
                resolve(res.data)
            } else if (res.data.Code == 500) {

                resolve(res.data)
            }
        }).catch(err => {
            reject(err.data)
        })
    })
}
//暴露
export default request;